package com.hoshiicloud.saleorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.entity.Payorder;
import com.hoshiicloud.saleorder.param.PayorderAddParam;
import com.hoshiicloud.saleorder.param.PayorderQueryParam;
import com.hoshiicloud.saleorder.param.PayorderUpdateParam;
import com.hoshiicloud.saleorder.service.PayorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 支付订单 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/saleorder/payorder")
@Api(value = "支付订单", tags = {"支付订单"})
public class PayorderController {

    @Autowired
    private PayorderService payorderService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询支付订单
     */
    @ApiOperation(value = "分页查询支付订单")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PayorderQueryParam param) {
        log.info("分页查询支付订单");
        QueryWrapper<Payorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getPayListId() != null, Payorder::getPayListId, param.getPayListId())
                .eq(param.getUserId() != null, Payorder::getUserId, param.getUserId())
                .eq(StringUtils.isNotBlank(param.getPayOrderNo()), Payorder::getPayOrderNo, param.getPayOrderNo())
                .eq(param.getType() != null, Payorder::getType, param.getType())
                .eq(param.getPayTime() != null, Payorder::getPayTime, param.getPayTime())
                .eq(param.getPayAmount() != null, Payorder::getPayAmount, param.getPayAmount())
                .eq(param.getStatus() != null, Payorder::getStatus, param.getStatus());
        IPage<Payorder> page = new Page<>(Long.parseLong(param.getPageNumber()),
                Long.parseLong(param.getPageSize()));
        // queryWrapper.select(BeanUtils.getSqlSelect(Payorder.class));
        String key =
                "PayorderQueryParam{" + param.toString() + "} ,Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
        return Response.data(payorderService.page(page, queryWrapper, key));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询支付订单
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询支付订单")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Payorder> queryById(@PathVariable("id") Long id) {
        return Response.data(payorderService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增支付订单")
    public Response save(@RequestBody @Valid PayorderAddParam param) {
        Payorder entity;
        try {
            entity = BeanUtils.covertType(param, Payorder.class);
        } catch (Exception e) {
            log.error("新增支付订单异常 {}", e.getMessage());
            throw new ServiceException("新增支付订单异常");
        }
        return Response.data(payorderService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改支付订单
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改支付订单")
    public Response update(@RequestBody @Valid PayorderUpdateParam param) {
        Payorder entity;
        try {
            entity = BeanUtils.covertType(param, Payorder.class);
        } catch (Exception e) {
            log.error("修改支付订单类型转换异常", e);
            throw new ServiceException("修改支付订单异常");
        }
        return Response.data(payorderService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除支付订单
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除支付订单")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(payorderService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询支付订单")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.sort(null);
            return Response.data(payorderService.getByIds(ids, StringUtils.join(ids, "")));
        } else {
            return Response.data(null);
        }
    }

}
